package cn.kgc.ch1;

import redis.clients.jedis.Jedis;
import java.util.*;

public class Test2 {

    RedisUtil redisUtil=RedisUtil.getInstance();

    public static void main(String[] args) {
        Test2 test2 =new Test2();
       // test2.testStr();
//        test2.testMap();
      //  test2.testList();
       // test2.testSet();
        test2.testOrder();

    }

    // 字符串操作
    public void testStr() {
        Jedis jedis = redisUtil.getJedis();
        jedis.set("id", "15"); // 只能是字符串
        String id = jedis.get("id");
        System.out.println(id);
        System.out.println(jedis.setex("name", 10, "小明"));
        jedis.close();
    }

   // 操作 map  操作值为哈希结构的数据
    public void testMap() {
        Jedis jedis = redisUtil.getJedis();
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "xinxin");
        map.put("age", "22");
        map.put("qq", "123456");
        jedis.hmset("user", map);
        List<String> rsmap = jedis.hmget("user", "name", "age", "qq");
        System.out.println(rsmap);
        jedis.hdel("user", "age");
        Iterator<String> iter = jedis.hkeys("user").iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + ":" + jedis.hget("user", key));
        }
        jedis.close();
    }


    // 操作 list
    public void testList() {
        Jedis jedis = redisUtil.getJedis();
        jedis.del("java framework");
        System.out.println(jedis.lrange("java framework", 0, -1));
        jedis.lpush("java framework", "spring");
        jedis.lpush("java framework", "struts");
        jedis.lpush("java framework", "hibernate");
        System.out.println(jedis.lrange("java framework", 0, -1));
        jedis.del("java framework");
        jedis.rpush("java framework", "spring");
        jedis.rpush("java framework", "struts");
        jedis.rpush("java framework", "hibernate");
        System.out.println(jedis.lrange("java framework", 0, -1));
        jedis.close();
    } //
    //操作 set
    public void testSet() {
        Jedis jedis = redisUtil.getJedis();
        jedis.sadd("user1", "liuling");
        jedis.sadd("user1", "xinxin");
        jedis.sadd("user1", "ling");
        jedis.sadd("user1", "zhangxinxin");
        jedis.sadd("user1", "who");
        jedis.srem("user1", "who"); // 移除noname
        System.out.println(jedis.smembers("user1"));// 获取所有加入的value
        System.out.println(jedis.sismember("user1", "who"));// 判断 who
        System.out.println(jedis.srandmember("user1")); // 是否是user集合的元素
        System.out.println(jedis.scard("user1"));// 返回集合的元素个数
        jedis.close();
    }



    //jedis 排序
    public void testOrder() {
        Jedis jedis = redisUtil.getJedis();
        jedis.del("a");
        jedis.rpush("a", "1");
        jedis.lpush("a", "6");
        jedis.lpush("a", "3");
        jedis.lpush("a", "9");
//9361
        System.out.println(jedis.lrange("a", 0, -1));
        System.out.println(jedis.sort("a"));
        System.out.println(jedis.lrange("a", 0, -1));
        jedis.close();
    }


}
